﻿<li class="xtabs-item nav-item @_dropClass" draggable="@IsDraggable.ToString()" ondragover="event.preventDefault();" @ondragstart="@DragStartAsync" @ondragenter="DragEnter" @ondrop="@DragDropAsync" @ondragleave="DragLeave" @ondragend="DragEndAsync" @onmousedown="async (e) => await CloseTabOnMouseWheelAsync(XTab, e)">
    @if (CloseTabs)
    {
        <button type="button" @onclick="async () => await OnCloseTab.InvokeAsync(XTab)" class="xtabs-close close" aria-label="Close">
            <span aria-hidden="true">×</span>
        </button>
    }
    <a class='xtabs-link nav-link @(IsActive ? "active" : "")' @onclick="async () => { if (CanSetActive) { await OnSetActive.InvokeAsync(XTab);} }" 
       ondragover="event.preventDefault();" draggable="false">
        <h6 class="m-0 font-weight-bold text-primary" draggable="false">@XTab.Title</h6>
    </a>
</li>
@code {

    /// <summary>
    /// Can close tabs.
    /// </summary>
    [Parameter] public bool CloseTabs { get; set; }

    /// <summary>
    /// Gets or sets if tab header is active.
    /// </summary>
    [Parameter] public bool IsActive { get; set; }

    /// <summary>
    /// Gets or sets if tab header can change active on click
    /// </summary>
    [Parameter] public bool CanSetActive { get; set; }

    /// <summary>
    /// Gets or sets the related XTab
    /// </summary>
    [Parameter]
    public XTab XTab { get; set; }

    /// <summary>
    /// Event: Sets current tab header and related xtab as active
    /// </summary>
    [Parameter]
    public EventCallback<XTab> OnSetActive { get; set; }

    /// <summary>
    /// Event: Closes current tab header and related xtab
    /// </summary>
    [Parameter]
    public EventCallback<XTab> OnCloseTab { get; set; }

    /// <summary>
    /// Gets or sets the XTabs's drag feature.
    /// </summary>
    [Parameter]
    public bool IsDraggable { get; set; }

    /// <summary>
    /// Event: Current tab header started drag.
    /// </summary>
    [Parameter]
    public EventCallback<XTab> OnDragStart { get; set; }

    /// <summary>
    /// Event: Current tab header ended drag.
    /// </summary>
    [Parameter]
    public EventCallback<XTab> OnDragEnd { get; set; }

    /// <summary>
    /// Event: Current tab header dropped drag.
    /// </summary>
    [Parameter]
    public EventCallback<XTab> OnDragDrop { get; set; }

    private string _dropClass;

    private async Task DragStartAsync(DragEventArgs dragEventArgs)
    {
        if (!IsDraggable)
            return;
        await OnDragStart.InvokeAsync(XTab);
        return;
    }

    private async Task DragDropAsync(DragEventArgs dragEventArgs)
    {
        if (!IsDraggable)
            return;
        await OnDragDrop.InvokeAsync(XTab);
        _dropClass = string.Empty;
        return;
    }

    private async Task DragEndAsync(DragEventArgs dragEventArgs)
    {
        if (!IsDraggable)
            return;
        await OnDragEnd.InvokeAsync(XTab);
        return;
    }

    private void DragEnter()
    {
        if (!IsDraggable)
            return;
        _dropClass = "drop";
        return;
    }

    private void DragLeave()
    {
        if (!IsDraggable)
            return;
        _dropClass = string.Empty;
        return;
    }

    private async Task CloseTabOnMouseWheelAsync(XTab xTab, MouseEventArgs e)
    {
        if (e.Button == 1) await OnCloseTab.InvokeAsync(xTab);
    }
}